package com.sina.shop.service.collect;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.dao.collect.CollectBusinessDao;
import com.sina.shop.domain.collect.CollectBusiness;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-06-15:54
 */
@Service("collectBusinessService")
public class CollectBusinessServiceImpl extends ShopDataSourceService implements CollectBusinessService {

    @Autowired
    @Qualifier("collectBusinessDao")
    private CollectBusinessDao collectBusinessDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<CollectBusiness> getCollectBusinessByAdminId(Long adminId) {
        return collectBusinessDao.getCollectBusinessByAdminId(adminId);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Integer deleteCollectBusiness(Long id) throws DataException, UpdateException {
        CollectBusiness collectBusiness = collectBusinessDao.get(CollectBusiness.class, id);
        Assert.assertNotNull(collectBusiness, "CollectBusiness[id=%s] not exists", collectBusiness.getId());

        try {
            return collectBusinessDao.deleteCollectBusiness(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete CollectBusiness error.", CollectBusiness.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public CollectBusiness saveCollectBusiness(CollectBusiness collectBusiness) throws CreateException, ResourceExistsException, DataException {
        try {
            //如果已收藏过，则抛出异常
            if (collectBusiness.getBusiness() != null && collectBusiness.getAdmin() != null) {
                if (collectBusinessDao.getCollectBusinessByAidAndBid(
                        collectBusiness.getAdmin().getId(),
                        collectBusiness.getBusiness().getId()
                ) != null) {
                    throw new DataAccessResourceFailureException("收藏失败，你已收藏过！！");
                }
            }

            collectBusinessDao.save(collectBusiness);
            collectBusinessDao.flush();
            return collectBusiness;
        } catch (DataAccessException e) {
            LOG.error(" {} save CollectBusiness error.", CollectBusiness.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectBusiness getCollectBusinessByAidAndBid(Long adminId, Long businessId) {
        return collectBusinessDao.getCollectBusinessByAidAndBid(adminId, businessId);
    }
}
